package controllClient;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import controllBen.Eventos;
import controllServer.EventosControll;

/**
 * Servlet implementation class AtualizaEventosServlet
 */
public class AtualizaEventosServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private EventosControll controll;

	public AtualizaEventosServlet() {
		super();

		try {
			controll = new EventosControll();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String action = request.getParameter("action");
		System.out.println(action);

		if ("update".equals(action)) {
			Eventos evento = new Eventos();

			evento.setId(Integer.parseInt(request.getParameter("id")));
			evento.setTitulo(request.getParameter("titulo"));
			evento.setDescricao(request.getParameter("descricao"));

			controll.update(evento);
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}

		if ("delete".equals(action)) {
			int id = Integer.parseInt(request.getParameter("id_delete"));
			System.out.println(id);
			controll.delete(id);
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}

		if ("insert".equals(action)) {

			Eventos evento = new Eventos();

			evento.setTitulo(request.getParameter("titulo"));
			evento.setDescricao(request.getParameter("descricao"));

			controll.insert(evento);
			request.getRequestDispatcher("adicionar_evento.html").forward(request, response);
		}
	}

}
